import axios from 'axios'
import { Notify } from 'vant';
import router from '@/router'
import { mapState } from 'vuex'

axios.defaults.baseURL = `http://192.168.2.166:5000`

// 请求拦截  设置统一header
axios.interceptors.request.use(config => {
  // 加载
  // startLoading()
  if (localStorage.eleToken)
    config.headers.Authorization = localStorage.eleToken
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截  401 token过期处理
axios.interceptors.response.use(response => {
  // endLoading()
  return response
}, error => {
  // 错误提醒
  // endLoading()
  Notify({ type: 'danger', message: error.response.data })

  const { status } = error.response
  if (status == 401) {
    Notify({ type: 'danger', message: 'token值无效，请重新登录' })
    // 清除token
    localStorage.removeItem('eleToken')

    // 页面跳转
    router.push('/login')
  }

  return Promise.reject(error)
})

export default axios